package name.gauravmadan.mdg.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.gauravmadan.mdg.generator.CodeGenerator;
import name.gauravmadan.mdg.pim.Application;
import name.gauravmadan.mdg.service.ModelDataService;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ApplicationListController extends MultiActionController{
	private ModelDataService service;
	private CodeGenerator codeGenerator;
	
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
	{
		List<Application> applicationList = service.getApplicationList();
		ModelAndView mav = new ModelAndView("application-list");
		mav.addObject("applicationList", applicationList);
		return mav;
	}
	public ModelAndView showFormNew(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("application-form");
		Application app = new Application();
		mav.addObject("application", app);
		return mav;
	}
	public ModelAndView showFormEdit(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedApplicationId = request.getParameter("selectedApplicationId");
		// Load Application Object based on selected application id
		System.out.println("SELECTED APPLICATION ID IS: " + selectedApplicationId);
		ModelAndView mav = new ModelAndView("application-form");
		Application app = null;
		if (selectedApplicationId != null && !"".equals(selectedApplicationId)) {
			app = service.getApplicationById(new Integer(selectedApplicationId));
		}
		else {
			// Return error here
		}
		mav.addObject("application", app);
		return mav;
	}
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedApplicationId = request.getParameter("selectedApplicationId");
		if (selectedApplicationId != null && !"".equals(selectedApplicationId)) {
			service.deleteApplicationById(new Integer(selectedApplicationId));
		}
		
//		List<Application> applicationList = service.getApplicationList();
//		ModelAndView mav = new ModelAndView("application-list");
//		mav.addObject("applicationList", applicationList);
//		return mav;
		ModelAndView mav = new ModelAndView("redirect:/application-list.html?method=list");
		return mav;
	}
	public ModelAndView showComponents(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("redirect:/component-list.html?method=list");
		mav.addObject("parentApplicationId", request.getParameter("selectedApplicationId"));
		return mav;
	}
	public ModelAndView generateCode(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedApplicationId = request.getParameter("selectedApplicationId");
		if (selectedApplicationId != null && !"".equals(selectedApplicationId)) {
			codeGenerator.generate(new Integer(selectedApplicationId));
		}

		ModelAndView mav = new ModelAndView("redirect:/application-list.html?method=list");
		return mav;
	}
	
	public ModelDataService getService() {
		return service;
	}
	public void setService(ModelDataService service) {
		this.service = service;
	}
	public CodeGenerator getCodeGenerator() {
		return codeGenerator;
	}
	public void setCodeGenerator(CodeGenerator codeGenerator) {
		this.codeGenerator = codeGenerator;
	}
	
}
